package system;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


@WebServlet("/studentsFinishTest")
public class studentsFinishTest extends HttpServlet {
	private static final long serialVersionUID = 1L;
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int type=Integer.parseInt(req.getParameter("type"));	
    	int partNo=Integer.parseInt(req.getParameter("partNo"));
		int questionNo=Integer.parseInt(req.getParameter("questionNo"));
		String answer=req.getParameter("answer");
		String answer_A=req.getParameter("answer_A");
		String answer_B=req.getParameter("answer_B");
		String answer_C=req.getParameter("answer_C");
		String answer_D=req.getParameter("answer_D");
		
		HttpSession session=req.getSession();
        String testid=(String)session.getAttribute("testid");
        String userid=(String)session.getAttribute("userid");
		String result="";
		
		sqlOperation sql = new sqlOperation();
		
		if(type==1) {
			int insert = sql.getInsert("insert into student_question_single_selection values('"+userid+"','"+testid+"','"+partNo+"','"+questionNo+"','"+answer+"','0')");
			String trueAnswer = sql.getQuerySet("select selection_true from single_selection where question_id=(select question_id from questions where test_id='"+testid+"' and part_no='"+partNo+"' and question_no='"+questionNo+"')",1)[0];
			if(trueAnswer.equals(answer)) {
				insert+=sql.getUpdate("update student_question_single_selection set grade=(select per_point from parts where test_id='"+testid+"' and part_no='"+partNo+"') where student_id='"+userid+"' and test_id='"+testid+"' and part_no='"+partNo+"' and question_no='"+questionNo+"'");
			    
			}
			insert+=sql.getUpdate("update student_test set grade=(select grade from student_question_single_selection where student_id='"+userid+"' and test_id='"+testid+"' and part_no='"+partNo+"' and question_no='"+questionNo+"')+(select grade from student_test where test_id='"+testid+"' and student_id='"+userid+"') where test_id='"+testid+"' and student_id='"+userid+"'");
			result = "{\"code\":\""+insert+"\"}";
		}else if(type==2) {
			int insert = sql.getInsert("insert into student_question_multiple_selection values('"+userid+"','"+testid+"','"+partNo+"','"+questionNo+"','"+answer_A+"','"+answer_B+"','"+answer_C+"','"+answer_D+"','0')");
			String trueAnswer_A="",trueAnswer_B="",trueAnswer_C="",trueAnswer_D="";
			String[] trueAnswers = sql.getQueryLine("select selection_true_A,selection_true_B,selection_true_C,selection_true_D from multiple_selection where question_id=(select question_id from questions where test_id='"+testid+"' and part_no='"+partNo+"' and question_no='"+questionNo+"')",4);
			if(answer_A==null){
				answer_A="null";
		    }
		    if(answer_B==null){
		    	answer_B="null";
		    }
		    if(answer_C==null){
		    	answer_C="null";
		    }
		    if(answer_D==null){
		    	answer_D="null";
		    }
		    if(trueAnswers[0].equals(answer_A)&&trueAnswers[1].equals(answer_B)&&trueAnswers[2].equals(answer_C)&&trueAnswers[3].equals(answer_D)) {
		    	insert+=sql.getUpdate("update student_question_multiple_selection set grade=(select per_point from parts where test_id='"+testid+"' and part_no='"+partNo+"') where student_id='"+userid+"' and test_id='"+testid+"' and part_no='"+partNo+"' and question_no='"+questionNo+"'");
		    }
		    insert+=sql.getUpdate("update student_test set grade=(select grade from student_question_multiple_selection where student_id='"+userid+"' and test_id='"+testid+"' and part_no='"+partNo+"' and question_no='"+questionNo+"')+(select grade from student_test where test_id='"+testid+"' and student_id='"+userid+"') where test_id='"+testid+"' and student_id='"+userid+"'");
			result = "{\"code\":\""+insert+"\"}";
		}else if(type==3) {
			int insert = sql.getInsert("insert into student_question_subjective_topic values('"+userid+"','"+testid+"','"+partNo+"','"+questionNo+"','"+answer+"','0')");
			insert+=sql.getInsert("insert into have_subject_topic values('"+userid+"','"+testid+"','"+partNo+"','"+questionNo+"','0')");
			result = "{\"code\":\""+insert+"\"}";
		}else if(type==4) {
			int update = sql.getUpdate("update student_test set isFinished='1' where student_id='"+userid+"' and test_id='"+testid+"'");
			result = "{\"code\":\""+update+"\"}";
		}
		
		resp.setContentType("application/json;charset=UTF-8");
		PrintWriter out = resp.getWriter();		
		out.write(result);
		
    }
}
